package com.snake.test.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.snake.mylibrary.mina.MinaService;
import com.snake.mylibrary.mina.SessionManager;
import com.snake.mylibrary.utils.LogUtil;
import com.snake.test.R;

/**
 * mina框架测试，疑问？怎么查看是否是长连接状态。
 */
public class MinaActivity extends AppCompatActivity {

    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mina);
        LocalBroadcastManager.getInstance(this).registerReceiver(new MinaClientReceiver(),new IntentFilter("com.snake.mylibrary.mina"));
        mTextView = (TextView) findViewById(R.id.textView2);
    }

    public void send(View view){
        SessionManager.getInstance().writeToServer("来自mina 客户端的问候");
    }

    public void startService(View view){
        Intent intent = new Intent(this, MinaService.class);
        startService(intent);
        LogUtil.e("startService");
    }

    class MinaClientReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra("message");
            mTextView.setText(message);
        }
    }

}
